package io.github.biezhi.Strategy;

import java.util.Arrays;
import java.util.List;

/**
 * 建造者模式（Builder Pattern）是一种创建型设计模式，它将一个复杂对象的构建与它的表示分离，使得同样的构建过程可以创建不同的表示。这种模式适用于创建复杂的对象，特别是那些包含多个可选部分或者步骤的对象。
 */

// 定义策略接口（DiscountStrategy）
interface DiscountStrategy {
    double calculateDiscount(double originalPrice);
}

// 实现具体的策略类：FixedDiscountStrategy、PercentageDiscountStrategy 和 NoDiscountStrategy
class FixedDiscountStrategy implements DiscountStrategy {
    private final double fixedDiscountAmount;

    public FixedDiscountStrategy(double fixedDiscountAmount) {
        this.fixedDiscountAmount = fixedDiscountAmount;
    }

    @Override
    public double calculateDiscount(double originalPrice) {
        return originalPrice - fixedDiscountAmount;
    }
}

class PercentageDiscountStrategy implements DiscountStrategy {
    private final double discountPercentage;

    public PercentageDiscountStrategy(double discountPercentage) {
        this.discountPercentage = discountPercentage;
    }

    @Override
    public double calculateDiscount(double originalPrice) {
        return originalPrice * (1 - discountPercentage / 100);
    }
}

class NoDiscountStrategy implements DiscountStrategy {
    @Override
    public double calculateDiscount(double originalPrice) {
        return 0; // 不打折，返回0作为折扣金额
    }
}

// 上下文类（Context），使用策略对象来执行计算
class ShoppingCart {
    private List<Item> items;
    private DiscountStrategy discountStrategy;

    public ShoppingCart(List<Item> items, DiscountStrategy discountStrategy) {
        this.items = items;
        this.discountStrategy = discountStrategy;
    }

    public double getTotalPriceWithDiscount() {
        double totalPrice = items.stream().mapToDouble(Item::getPrice).sum();
        double discount = discountStrategy.calculateDiscount(totalPrice);
        return totalPrice - discount;
    }
}

// 示例数据类
class Item {
    private String name;
    private double price;

    public Item(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public double getPrice() {
        return price;
    }
}

// 客户端使用
public class DiscountStrategyClient {
    public static void main(String[] args) {
        List<Item> shoppingCartItems = Arrays.asList(
                new Item("Product A", 100),
                new Item("Product B", 200),
                new Item("Product C", 300)
        );

        // 使用不同的折扣策略计算总价
        ShoppingCart cartWithFixedDiscount = new ShoppingCart(shoppingCartItems, new FixedDiscountStrategy(50));
        System.out.println("Total price with fixed discount: " + cartWithFixedDiscount.getTotalPriceWithDiscount());

        ShoppingCart cartWithPercentageDiscount = new ShoppingCart(shoppingCartItems, new PercentageDiscountStrategy(10));
        System.out.println("Total price with percentage discount: " + cartWithPercentageDiscount.getTotalPriceWithDiscount());

        ShoppingCart cartWithNoDiscount = new ShoppingCart(shoppingCartItems, new NoDiscountStrategy());
        System.out.println("Total price with no discount: " + cartWithNoDiscount.getTotalPriceWithDiscount());
    }
}
